//
//  PKSideMenuViewController.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/5/9.
//

import UIKit
import RxSwift
import RxCocoa
import NSObject_Rx
import RxDataSources
import SnapKit

class PKSideMenuViewController: PKBaseViewController , NavigationBarConfigurable {
    
    var shouldHideNavigationBar : Bool { true}
//    var showBackButton : Bool { false }
    private var tableView : UITableView!
    private var loginButton : PopSpringRippleButton!
    private var lineView : UIView!
    private var titleLabel = UILabel()
    //    private    var backButton = UIButton(type: .custom)
    private var iconImageView : UIImageView!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        updateNavBarVisibility()
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    override func setupUI() {
        setupNavigationBar()
        // Do any additional setup after loading the view.
        view.backgroundColor = .white
        
        iconImageView = UIImageView()
        iconImageView.backgroundColor = .green
        view.addSubview(iconImageView)
        
        titleLabel.text = "Ghiblitattoo"
        titleLabel.font = .adaptedWightFont(size: 18, weight: .bold)
        view.addSubview(titleLabel)
        tableView = UITableView(frame: .zero, style: .grouped)
        tableView.separatorStyle = .none
        tableView.register(UINib(nibName: "PKSideMenuTableViewCell", bundle: nil), forCellReuseIdentifier: "PKSideMenuTableViewCell")
        view.addSubview(tableView)
        tableView.backgroundColor = .white
        tableView.isScrollEnabled = false
        
        loginButton = PopSpringRippleButton(frame:.zero)
        loginButton.setTitle("Login", for: .normal)
        loginButton.layer.cornerRadius  = 17
//        loginButton.clipsToBounds = true
//        loginButton.clipsToBounds = true
        loginButton.backgroundColor = .white
        loginButton.titleLabel?.font = .adaptedWightFont(size: 17, weight: .semibold)
//        loginButton.titleLabel?.textColor = .black
        loginButton.setTitleColor(.black, for: .normal)
        view.addSubview(loginButton)
        
        lineView = UIView()
        lineView.backgroundColor = .red
        view.addSubview(lineView)
        
        
    }
    
    override func bindEvents() {
        
        iconImageView.snp.makeConstraints { make in
            make.top.equalTo(DeviceHelper.navigationFullHeight+10)
            make.left.equalTo(ScreenAdapter.x(50))
            make.width.height.equalTo(ScreenAdapter.x(50))
        }
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(ScreenAdapter.x(10))
            make.centerY.equalTo(iconImageView)
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(iconImageView.snp.bottom).offset(ScreenAdapter.x(20))
            make.left.equalTo(ScreenAdapter.x(30))
            make.right.equalTo(-ScreenAdapter.x(30))
            make.height.equalTo(ScreenAdapter.x(400))
        }
        loginButton.snp.makeConstraints { make in
            make.left.equalTo(ScreenAdapter.x(50))
            make.bottom.equalTo(-DeviceHelper.bottomSafeAreaHeight - 20)
            make.width.equalTo(ScreenAdapter.x(120))
            make.height.equalTo(ScreenAdapter.x(34))
        }
        lineView.snp.makeConstraints { make in
            make.centerX.equalTo(loginButton)
            make.top.equalTo(loginButton.snp.bottom)
            make.height.equalTo(4)
            make.width.equalTo(20)
        }
        
    }
    
    override func bindViewModel() {
        let items = Observable.just([SectionModel(model: "", items: ["How to Use","Show Case","UserReviews","Subscription","FAQ","Blog","Mine"])])
        let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String,String>> (configureCell: {(datasource,tv,indexPath,element) in
            let cell = tv.dequeueReusableCell(withIdentifier: "PKSideMenuTableViewCell") as! PKSideMenuTableViewCell
            cell.titleLabel?.text = element
            return cell
        } )
        
        items.bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: rx.disposeBag)
        tableView.rx.setDelegate(self).disposed(by: rx.disposeBag)
        tableView.rx.modelSelected(String.self).subscribe { model in
            if model.element == "Subscription" {
                let price = PKPricingPlansViewController()
                self.navigationController?.pushViewController(price, animated: true)
//                let check = PKCheckOutViewController()
//                self.navigationController?.pushViewController(check, animated: true)
//                let pay = PKPaymentStoreViewController()
//                self.navigationController?.pushViewController(pay, animated: true)
//                let pay = PKPaymentStoreViewController()
//                self.navigationController?.pushViewController(pay, animated: true)
            }else if model.element == "FAQ" {
                let faq = PKFAQViewController()
                self.navigationController?.pushViewController(faq, animated: true)
            }else if model.element == "UserReviews" {
                let review = PKUserReviewsViewController()
                self.navigationController?.pushViewController(review, animated: true)
            }else if model.element == "Mine"{
                let mine = PKMineViewController.init(nibName: "PKMineViewController", bundle: nil)
                self.navigationController?.pushViewController(mine, animated: true)
            }
        }.disposed(by: rx.disposeBag)
        
        
        loginButton.rx.tap.subscribe { _ in
            let login = PKLoginViewController()
            self.navigationController?.pushViewController(login, animated: true)
        }.disposed(by: rx.disposeBag)
        
        
    }
    
}

extension PKSideMenuViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    
    
}


struct tableItem{
    let title : String
}
struct Section {
    var header : String
    var items : [Item]
}

extension Section : SectionModelType {
    typealias Item = tableItem
    init(original: Section, items: [tableItem]) {
        self = original
        self.items = items
    }
}
